function oi = orthimage(dm)
% orthimage  - compute orthogonality matrix (for SPM like display)
%
% FORMAT:       oi = orthimage(dm)
%
% Input fields:
%
%       dm      design matrix
%
% Output fields:
%
%       oi      orthogonality image

% Version:  v0.7a
% Build:    7080813
% Date:     Aug-08 2007, 1:52 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
   (~isa(dm, 'double') && ...
    ~isa(dm, 'single')) || ...
    ndims(dm) ~= 2 || ...
    isempty(dm)
    error( ...
        'BVQXtools:TooFewArguments',...
        'Too few arguments. Try ''help %s''.',...
        mfilename ...
    );
end

% covariance and vector length for cosine calculus
cm = dm' * dm;
dcm = sqrt(sum(dm .* dm));

% calculus
oi = abs(1 - abs(cm ./ (dcm' * dcm)));
oi(abs(oi) < (4 * eps)) = 0;
oi(abs(oi - 1) < (4 * eps)) = 1;
